package guis.visitant;

import exceptions.RepositoryException;
import guis.ComponentFactory;
import guis.ModalWindow;
import guis.RegisterPanel;

import java.awt.event.KeyEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JTextField;

import classes.Classroom;
import classes.Visitant;

import communication.Facade;

public class VisitantWindow extends ModalWindow
{
	private static final long			serialVersionUID	= 1032307379765488755L;
	protected JTextField				nameTextField;
	protected JComboBox					dayComboBox;
	protected JComboBox					monthComboBox;
	protected JComboBox					yearComboBox;
	protected JComboBox					classroomComboBox;
	protected JButton					actionButton;
	protected JButton					cancelButton;
	protected RegisterPanel< Visitant >	registerPanel;
	protected Facade					facade;

	public VisitantWindow( String title, String actionButtonFunction, JFrame source, RegisterPanel< Visitant > registerPanel, Facade facade )
	{
		super( title, source );
		this.facade = facade;
		this.registerPanel = registerPanel;
		initialize();
		createLabels();
		createTextFields();
		createComboBoxes();
		createButtons( actionButtonFunction );
	}

	private void initialize()
	{
		this.setSize( 490, 130 );
		this.setLocationRelativeTo( null );
	}

	private void createLabels()
	{
		ComponentFactory.createLabel( "Nome:", 10, 10, getContentPane() );
		ComponentFactory.createLabel( "Dia da Visita:", 10, 40, getContentPane() );
		ComponentFactory.createLabel( "/", 175, 40, getContentPane() );
		ComponentFactory.createLabel( "/", 275, 40, getContentPane() );
		ComponentFactory.createLabel( "Sala:", 10, 70, getContentPane() );
	}

	private void createTextFields()
	{
		this.nameTextField = ComponentFactory.createJTextField( 250, 22, 100, 10, getContentPane() );
	}

	private void createComboBoxes()
	{
		Vector< Classroom > classrooms = new Vector< Classroom >();
		try
		{
			classrooms = facade.getIteratorClassroom().toVector();
			classrooms.add( 0, null );
		}
		catch ( RepositoryException e )
		{
		}
		this.classroomComboBox = ComponentFactory.createJComboBox( classrooms, 250, 22, 100, 70, getContentPane() );

		Integer[] days = new Integer[ 32 ];
		Integer[] months = new Integer[ 13 ];
		Integer[] years = new Integer[ 43 ];

		for ( int i = 1; i < days.length; i++ )
			days[ i ] = i;
		for ( int i = 1; i < months.length; i++ )
			months[ i ] = i;
		for ( int i = 1; i < years.length; i++ )
			years[ i ] = i + 2008;

		this.dayComboBox = ComponentFactory.createJComboBox( days, 50, 22, 100, 40, getContentPane() );
		this.monthComboBox = ComponentFactory.createJComboBox( months, 50, 22, 200, 40, getContentPane() );
		this.yearComboBox = ComponentFactory.createJComboBox( years, 50, 22, 300, 40, getContentPane() );
	}

	private void createButtons( String actionButtonFunction )
	{
		this.actionButton = ComponentFactory.createJButton( actionButtonFunction, true, 110, 22, 365, 40, null, getContentPane() );
		this.cancelButton = ComponentFactory.createJButton( "cancelar", true, 110, 22, 365, 70, null, getContentPane() );

		if ( actionButtonFunction.equals( "inserir" ) )
			actionButton.setMnemonic( KeyEvent.VK_I );
		else
			actionButton.setMnemonic( KeyEvent.VK_B );
		cancelButton.setMnemonic( KeyEvent.VK_C );
	}

	public void setVisible( boolean arg0 )
	{
		super.setVisible( arg0 );
		if ( !arg0 )
		{
			source.setEnabled( true );
			source.setVisible( true );
		}
	}
}
